# encoding: utf-8
"""
!/usr/bin/python3
@Author: Gao Shuo
@Time: 2019/3/20 11:19 
@ReadMe: 北京五环内街景，分析过各部分占比后，计算视觉熵
    Input: 各部分占比 0-result.txt
    Output: 
"""
import argparse
import math
import pandas as pd
import sys
import re

def make_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model', type=str, required=True, help='.prototxt file for inference')
    parser.add_argument('--weights', type=str, required=True, help='.caffemodel file')
    parser.add_argument('--colours', type=str, required=True, help='label colours')
    parser.add_argument('--input_dir', type=str, required=True, help='input 0-result path')
    parser.add_argument('--out_dir', type=str, default=None, help='output directory in which the segmented images '
                                                                   'should be stored')
    parser.add_argument('--layer_name',type=str,default='argmax',help='predicted layer name')
    parser.add_argument('--gpu', type=str, default='0', help='0: gpu mode active, else gpu mode inactive')

    return parser

def isnumber(num):
    regex = re.compile(r"^(-?\d+)(\.\d*)?$")
    if re.match(regex,num):
        return True
    else:
        return False

def visual_canopy(x):
    res = 0
    for xi in x:
        # print xi
        if isnumber(str(xi) ):#过滤掉首列 图片名
            if xi > 0:
                res += -(xi * math.log(xi))
    return res


if __name__ == '__main__':
    path = sys.argv[1]
    dest = sys.argv[2]
    arr_title = ['img','Sky',	'Building',	'Pole', 'Road_marking','Road', 'Pavement', 'Tree', 'SignSymbol', 'Fence', 'Car', 'Pedestrian',
                 'Bicyclist', 'Unlabelled']
    df = pd.read_csv(path, sep='\t', header=None)
    df.columns =arr_title #加列名

    df['visual_canopy'] = df.apply(lambda s: visual_canopy(s), axis=1)
    df.to_csv(dest,sep='\t', index=False)


